from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from sku.models import SPU
from group.models import Cate
from goods.models import Brand
from sku.serializers import SpuSer
from group.serializers import CateSer
from goods.serializers import BrandSer
# Create your views here.
class AllSpuView(APIView):
    def get(self,request):
        page = int(request.query_params.get('page'))
        size = int(request.query_params.get('size'))
        data = SPU.objects.all()[((page-1)*size):(page*size)]
        ser = SpuSer(data,many=True)
        return Response({'data':ser.data,'count':SPU.objects.all().count()})

# 一级二级分类
class AllCate(APIView):
    def get(self,request):
        data = Cate.objects.filter(cid_id=None)
        ser = CateSer(data,many=True)
        return Response(ser.data)


# 三级分类
class GetCate(APIView):
    def get(self,request):
        data = Cate.objects.all()
        ser = CateSer(data,many=True)
        return Response(ser.data)


class GetBrand(APIView):
    def get(self,request):
        data = Brand.objects.all()
        ser = BrandSer(data,many=True)
        return Response(ser.data)

class OneSpuView(APIView):
    def get(self,request,id):
        try:
            data = SPU.objects.get(id=id)
            ser = SpuSer(data)
            return Response(ser.data)
        except Exception as e:
            print(e)
            return Response({'msg':'没有该数据','code':204})
    def put(self,request,id):
        name = request.data.get('name')
        brand_id = request.data.get('brand_id')
        category1_id = request.data.get('category1_id')
        category2_id = request.data.get('category2_id')
        category3_id = request.data.get('category3_id')
        desc_detail = request.data.get('desc_detail')
        desc_pack = request.data.get('desc_pack')
        desc_service = request.data.get('desc_service')
        try:
            SPU.objects.filter(id=id).update(
                name=name,
                brand_id = brand_id,
                category1_id=category1_id,
                category2_id=category2_id,
                category3_id=category3_id,
                desc_detail = desc_detail,
                desc_pack = desc_pack,
                desc_service = desc_service
            )
            return Response({'msg': '修改成功', 'code': 200})
        except Exception as e:
            print(e)
            return Response({'msg':'修改失败','code':204})
    def delete(self,request,id):
        try:
            SPU.objects.get(id=id).delete()
            return Response({'msg': '删除成功', 'code': 200})
        except Exception as e:
            print(e)
            return Response({'msg':'删除失败','code':204})


class AddSpuView(APIView):
    def post(self,request):
        name = request.data.get('name')
        brand_id = request.data.get('brand')
        category1_id = request.data.get('category1')
        category2_id = request.data.get('category2')
        category3_id = request.data.get('category3')
        desc_detail = request.data.get('desc_detail')
        desc_pack = request.data.get('desc_pack')
        desc_service = request.data.get('desc_service')
        try:
            SPU.objects.get(name=name)
            return Response({'msg': '该类商品已存在', 'code': 204})
        except Exception as e:
            SPU.objects.create(
                name=name,
                brand_id=brand_id,
                category1_id=category1_id,
                category2_id=category2_id,
                category3_id=category3_id,
                desc_service=desc_service,
                desc_pack=desc_pack,
                desc_detail=desc_detail
            )
            return Response({'msg': '添加成功', 'code': 200})